<?php
/**
* @version		$Id: dynl_dynlrec.php 1 2007-01-11 22:03:46Z friesengeist $
* @package		Dynlist
* @copyright	Copyright (C) 2005 - 2007 Dynlist. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* Dynlist is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
/**
* Dynlist data record class
*
* This class manages the data record of Dynlist.
*
* @package		Dynlist
* @final
*/
class dynl_datarec
{
	/**
	 * Dynlist name.
	 *
	 * @var char
	 * @access protected
	 */
	var $_dynlname = null;
	
	/**
	 * Database instance.
	 *
	 * @var object db_catalog object
	 * @access private
	 */
	var $_database;
	
	/**
	 * HTML instance.
	 *
	 * @var object html_catalog object
	 * @access private
	 */
	var $_html;

	/**
	 * Key fields list
	 * 
	 * @var array
	 * @access protected
	*/
	var $_keyfields = null;
	
	/**
	 * Key indexes
	 *
	 * @var array
	 * @access protected
	 */
	var $_keyindexes = null;
	
	/**
	 * Dynlist data.
	 *
	 * @var array
	 * @access protected
	 */
	var $_data = null;

	/**
	 * Sctructure used to print headers.
	 *
	 * @var object Dynl_Structure object
	 * @access protected
	 */
	var $_structure = null;
	
	/**
	* Class constructor
	*
	* @access public
	* @param char $key_char
	*/
	function dynl_datarec($dynlname)
	{
		$this->_dynlname = $dynlname;
		$this->_database = new dynl_database($dynlname);
		$this->_html = new html_datarec();
		$this->_structure = new dynl_structure($dynlname);
		
		$this->_keyfields = $this->_structure->get_keyfields();
		$this->_html->set_dynlname($this->_dynlname);
	}

	/**
	* Invocated HTML render.
	*
	* @access public
	*/
	function show_data()
	{
		echo '<a href="javascript:history.go(-1)">Voltar</a>';
	}

	/**
	* Manage the creation of new record.
	*
	* @access public
	*/
	function new_record()
	{
		// Set dynlist header texts.
		$this->_html->set_labels($this->_structure->get_texts());
		
		// Set keys fields.
		$this->_html->set_keyfields($this->_structure->get_keyfields());
		
		// As we are adding new record, there is no values to show.
		// Anyway, we must have to pass a array with one item that content
		// an array with field names.
		$content = array($this->_structure->get_fieldnames());
		$this->_html->set($content);
		
		// Render page;
		$this->_html->render_page('new_record');
	}

	/**
	* Manage the creation of new record.
	*
	* @access public
	* @param char $key_char
	*/
	function edit_record($key_char)
	{
                // Define filter based on key_char.
		$filter = $this->define_filter($key_char);
		//echo 'filter-->';
		//print_r($filter);
		//echo '<--filter';
		
		// Retrieve data from database.
                $this->_data = $this->_database->retrieve('all',$filter,'none');
		//echo 'data-->';
		//print_r($this->_data[0]);
		//echo '<--data';
		
		// Set dynlist header texts.
		$this->_html->set_labels($this->_structure->get_texts());
		//echo 'texts------->';
		//print_r($this->_structure->get_texts());
		
		// Set keys fields.
		$this->_html->set_keyfields($this->_structure->get_keyfields());
		
		// Set field values to edit.
		$this->_html->set($this->_data);
		
		// Render page;
		$this->_html->render_page('edit_record');

	}

	/**
	* Delete intire record.
	*
	* @access public
	*/
	function delete_record($key_char)
	{
                // Define filter based on key_char.
		$filter = $this->define_filter($key_char);
		//echo 'filter-->';
		//print_r($filter);
		//echo '<--filter';
		
		// Retrieve data from database.
                $this->_data = $this->_database->delete($filter);
		//echo 'data-->';
		//print_r($this->_data[0]);
		//echo '<--data';
	}
	
	/**
	* Save record on database
	*
	* @access public
	*/
	function save_record()
	{
		
	}
	
	/**
	* Ignore all changes on record and return to data list.
	*
	* @access public
	*/
	function cancel()
	{
	
	}

	/**
	 * Define filter based on key char from control.
	 *
	 * @access protected
	 * @param key_char
	 */
	function define_filter($key_char)
	{
		// define key values from key char from control.
		$keyvalues = spliti(";",$key_char);
		//echo 'keychar--->'.$key_char;
		//print_r($keyvalues);
		
		$i = 0;
		$j = 0;

		$keyfields = $this->_structure->get_keyfields();
		//print_r($keyfields);
		
		foreach ($keyfields as $h_keyfields)
		{
			$filter[$i] = $h_keyfields;
			$filter[$i+1] = $keyvalues[$j];
			$i = $i + 2;
			$j++;
		}
		//print_r($filter);
		return $filter;
	}

	
        /**
         * Set keyindexes.
         *
         * @access proteced
         */
        function set_keyindexes($keyindexes)
        {
                $this->_keyindexes = $keyindexes;
	}			    
}
?>
